feat(config): add reserved_context_size setting for configurable compaction trigger#639
Conversation
…paction trigger Allow users to configure the context usage threshold (0.1-1.0) that triggers auto-compaction. Default is 0.8 (80%). This replaces the previous fixed reserved_tokens approach with a percentage-based threshold. Fixes MoonshotAI#351 Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR introduces a configurable auto_compact_threshold setting to allow users to control when automatic context compaction triggers based on percentage of context usage, replacing the previous fixed token-based approach.
Changes:
- Added
auto_compact_thresholdconfiguration field (default: 0.8, range: 0.1-1.0) toLoopControl - Replaced fixed
RESERVED_TOKENSconstant with percentage-based threshold in compaction logic - Added validation tests for the new configuration parameter
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/kimi_cli/config.py | Adds auto_compact_threshold field to LoopControl with validation constraints |
| src/kimi_cli/soul/kimisoul.py | Removes RESERVED_TOKENS constant and updates compaction logic to use percentage threshold |
| tests/test_config.py | Adds tests for default value and boundary validation of auto_compact_threshold |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ): | ||
| logger.info("Context too long, compacting...") | ||
| threshold = self._loop_control.auto_compact_threshold | ||
| if self._context_usage >= threshold: |
There was a problem hiding this comment.
The code references self._context_usage but this property is not defined in the visible diff or the provided context. Verify that this property exists and correctly calculates the context usage percentage relative to self._runtime.llm.max_context_size.
|
What about |
|
Thanks for the feedback! You're right - the reserved token count is more important than the percentage, since the LLM needs a consistent amount of space for generation regardless of the total context size. I can update the PR to use class LoopControl(BaseModel):
# ...
reserved_context_size: int = Field(default=50_000, ge=1000)
"""Reserved token count that triggers auto-compaction. Default is 50000."""And the compaction trigger would be: if self._context.token_count + self._loop_control.reserved_context_size >= self._runtime.llm.max_context_size:This keeps the original logic but makes the reserved token count configurable. Should I push this change? |
Yeah sure. That would be sweet. Thank you! |
…eshold Per maintainer feedback, switch from percentage-based threshold to token-based reserved_context_size for auto-compaction. This better reflects what matters: ensuring enough tokens remain for LLM response generation regardless of total context size. Changes: - Rename auto_compact_threshold to reserved_context_size (default: 50000) - Restore original token-based compaction logic - Update tests accordingly Co-Authored-By: Claude <noreply@anthropic.com>
|
@stdrc Done! Pushed commit 8d0bd0b with the changes:
The config usage is now: [loop_control]
reserved_context_size = 50000 # can adjust based on needs |
…hold Signed-off-by: Richard Chien <stdrc@outlook.com>
Signed-off-by: Richard Chien <stdrc@outlook.com>
Related Issue
Resolve #351
Description
This PR adds a configurable
auto_compact_thresholdsetting that allows users to adjust when auto-compaction triggers.Changes:
auto_compact_thresholdfield toLoopControlconfig with default value 0.8 (80%)KimiSoulto use percentage-based threshold instead of the previous fixed reserved_tokens approachUsage:
Users can configure the threshold in their
config.toml:This addresses the user request to be able to adjust the auto-compact trigger point based on their task requirements.
Checklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.🤖 Generated with Claude Code